package org.jeecg.modules.readbook.controller;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class ScriptExtractor {

    public static void main(String[] args) {
        for (int i = 778; i < 999; i++) {
            readCoent(""+i);
        }
//        String content = "这是要写入txt文件的内容。\n这是第二行内容。";
//
//        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:\\01\\book2.txt"))) {
//            bufferedWriter.write(content);
//            System.out.println("使用BufferedWriter写入文件成功。");
//        } catch (IOException e) {
//            System.out.println("使用BufferedWriter写入文件时发生错误。");
//            e.printStackTrace();
//        }

    }

    public static void readCoent(String urlInde) {
        //String[] urls = {"165588/43273780.html","165588/43273781.html","165588/43273782.html"};
        //String url = "https://www.zheyueliang.com/go/165588/43273783.html";
        //String url2 = "https://www.zheyueliang.com/go/165588/43273778.html";

        String url = "https://www.zheyueliang.com/go/165588/43273"+urlInde+".html";
        try {
            // 1. 连接并获取网页内容（带浏览器伪装）
            Document doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
                    .timeout(10000)
                    .get();

            // 2. 定位目标div（注意HTML结构可能变化）
            Element targetDiv = doc.selectFirst("div.info_dv1.ov.y");

            if (targetDiv != null) {
                // 3. 提取div下所有script标签
                Elements scripts = targetDiv.getElementsByTag("script");

                // 4. 输出结果
                //System.out.println("找到 " + scripts.size() + " 个script标签：");
                for (int i = 0; i < scripts.size(); i++) {
                    Element script = scripts.get(i);
                    //System.out.println("\n--- Script " + (i+1) + " ---");

                    // 优先提取内联脚本内容
                    if (script.html().trim().length() > 0) {
                        //System.out.println(script.html());
                        String scriptContent = script.html();
                        int startIndex = scriptContent.indexOf("'");
                        int endIndex = scriptContent.indexOf("'", startIndex + 1);
                        if (startIndex != -1 && endIndex != -1) {
                            String contentBetweenQuotes = scriptContent.substring(startIndex + 1, endIndex);
                            //System.out.println("截取内容: " + contentBetweenQuotes);
                            String content = Base64Decoder.decode(contentBetweenQuotes);
                            System.out.println(content.replace("<p>", "").replace("</p>",""));

                        }
                        //String content = Base64Decoder.decode(script.html().replaceAll("document.writeln\\(", "").replaceAll("'\\)\\)\\;", ""));
                        //System.out.println(content);
                    }
                    // 如果没有内联内容，尝试获取src属性
                    else if (script.hasAttr("src")) {
                        System.out.println("外部脚本: " + script.attr("abs:src"));
                    }
                }

                //5 如果不需要解码
                Elements ps = targetDiv.getElementsByTag("p");
                //System.out.println("找到 " + ps.size() + " 个p标签：");
                for (int i = 1; i < ps.size()-1; i++) {
                    Element p = ps.get(i);
                    //System.out.println("\n--- Script " + (i+1) + " ---");

                    // 优先提取内联脚本内容
                    if (p.html().trim().length() > 0) {
                        //System.out.println(script.html());
                        String scriptContent = p.html();
                        System.out.println(scriptContent);
                    }
                    // 如果没有内联内容，尝试获取src属性
                    else if (p.hasAttr("src")) {
                        System.out.println("外部脚本: " + p.attr("abs:src"));
                    }
                }

            } else {
                System.out.println("未找到目标div元素");
            }

        } catch (IOException e) {
            System.err.println("请求失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}